package com.app.service.impl;

import com.app.bean.*;
import com.app.core.ReturnVoCommon;
import com.app.dao.MoviesMapper;
import com.app.dao.OrdersMapper;
import com.app.dao.TicketMapper;
import com.app.service.MoviesService;
import com.app.util.Const;
import com.app.util.StringUtilsEx;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 师晓峰
 * @version V1.0
 * @date 2023/9/21 20:06
 * @Description:
 */
@Service
public class MoviesServiceImpl implements MoviesService {

    @Autowired
    private MoviesMapper moviesMapper;

    @Autowired
    private TicketMapper ticketMapper;
    @Autowired
    private OrdersMapper ordersMapper;


    @Override
    public List<Movies> list(String moviename) {
        MoviesExample moviesExample = new MoviesExample();
        MoviesExample.Criteria criteria = moviesExample.createCriteria();
        moviesExample.setOrderByClause("id desc");
        if (StringUtilsEx.isNotEmpty(moviename)){
            criteria.andMovienameLike("%"+moviename+"%");
        }
        List<Movies> moviesList = moviesMapper.selectByExample(moviesExample);
        return moviesList;
    }

    @Override
    public List<Movies> hotList(String moviename) {
        MoviesExample moviesExample = new MoviesExample();
        MoviesExample.Criteria criteria = moviesExample.createCriteria();
        moviesExample.setOrderByClause("id desc");
        if (StringUtilsEx.isNotEmpty(moviename)){
            criteria.andMovienameLike("%"+moviename+"%");
        }
        criteria.andIsenjoyIsNull();
        List<Movies> moviesList = moviesMapper.selectByExample(moviesExample);
        return moviesList;
    }

    @Override
    public List<Movies> maxList() {
        MoviesExample moviesExample = new MoviesExample();
        MoviesExample.Criteria criteria = moviesExample.createCriteria();
        moviesExample.setOrderByClause("boxoffice desc");
        PageHelper.startPage(1,5);
        criteria.andIsenjoyIsNull();
        List<Movies> moviesList = moviesMapper.selectByExample(moviesExample);
        return moviesList;
    }

    /**
     *  根据类型查询所有电影信息
     * @param type
     * @return
     */
    @Override
    public List<Movies> query(String type) {
        MoviesExample moviesExample = new MoviesExample();
        MoviesExample.Criteria criteria = moviesExample.createCriteria();
        moviesExample.setOrderByClause("id desc");
        if (type.contentEquals("isenjoy")){
            criteria.andIsenjoyEqualTo("1");
        }else if(type.contentEquals("isesteem")){
            criteria.andIsesteemEqualTo("1");
        }else if(type.contentEquals("iscommercial")){
            criteria.andIscommercialEqualTo("1");
        }
        PageHelper.startPage(1,5);
        List<Movies> moviesList = moviesMapper.selectByExample(moviesExample);
        return moviesList;
    }

    @Override
    public Movies getById(String id) {
        return moviesMapper.selectByPrimaryKey(Integer.parseInt(id));
    }

    @Override
    public int add(Movies movies) {
        return moviesMapper.insert(movies);
    }

    @Override
    public int update(Movies movies) {
        return moviesMapper.updateByPrimaryKey(movies);
    }

    /**
     * 属性设置
     *  是否 最受期待
     *  是否 好评如潮
     *  是否 今日票房
     * @param id
     * @param column
     */
    @Override
    public void update(String id, String column){
        Movies movies = getById(id);
        if (column.equals("isenjoy")){
            movies.setIsenjoy("1");
        }else if(column.equals("isesteem")){
            movies.setIsesteem("1");
        }else if(column.equals("iscommercial")){
            movies.setIscommercial("1");
        }
        movies.setUpdatetime(new Date());
        moviesMapper.updateByPrimaryKey(movies);
    }

    @Override
    public List<Ticket> getTicketsByMovieId(String movieId) {
        TicketExample ticketExample = new TicketExample();
        TicketExample.Criteria criteria = ticketExample.createCriteria();
        if (StringUtilsEx.isNotEmpty(movieId)){
            criteria.andMovieidEqualTo(Integer.parseInt(movieId));
        }
        List<Ticket> ticketList = ticketMapper.selectByExample(ticketExample);
        return ticketList;

    }

    @Override
    public void createTickets(String movieId) {
        Movies movies = moviesMapper.selectByPrimaryKey(Integer.parseInt(movieId));
        for(int i = 0; i < Const.TICKETS_NUM; i++){
            Ticket ticket = new Ticket();
            ticket.setMovieid(Integer.parseInt(movieId));
            ticket.setMoviename(movies.getMoviename());
            ticket.setOccupy("空余座位");
            ticket.setSeat((i+1)+"座");
            ticket.setStatus("待售票");
            ticketMapper.insert(ticket);
        }
    }

    @Override
    public int delete(String id) {
        return moviesMapper.deleteByPrimaryKey(Integer.parseInt(id));
    }

    /**
     * 根据电影票id生成一个订单
     * @param id
     */
    @Override
    public void addOrder(String id){
        ReturnVoCommon vo = new ReturnVoCommon();
        // 先改变电影票的状态
        Ticket ticket = ticketMapper.selectByPrimaryKey(Integer.parseInt(id));
        ticket.setOccupy("售出座位");
        ticketMapper.updateByPrimaryKey(ticket);
        // 生成新的订单
        Orders orders = new Orders();
        orders.setMovieid(ticket.getMovieid());
        orders.setMoviename(ticket.getMoviename());
        orders.setSeat(ticket.getSeat());
        orders.setCreatetime(new Date());
        // 已经定票但是没付钱
        orders.setStatus("0");
        ordersMapper.insert(orders);
    }

}
